package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.OrderSetting;
import com.itheima.entity.Result;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 文件上传
     * @param excelFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(MultipartFile excelFile){

        try {
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            List<OrderSetting> list = new ArrayList<>();
            for (String[] strings : readExcel) {
                String orderDate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate),Integer.parseInt(number));
                list.add(orderSetting);
            }
            orderSettingService.add(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 文件下载
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //选择要下载的文件名
//        String fileName = request.getParameter("ordersetting_template.xlsx");
        //让浏览器收到这份资源时，以下载的方式提醒，而不是展示；
        response.setHeader("Content-Disposition", "attachment;filename=ordersetting_template.xlsx");

        ServletContext servletContext = request.getSession().getServletContext();

        String realPath = servletContext.getRealPath("ordersetting_template.xlsx");

        InputStream is = new FileInputStream(realPath) ;  //创建输入流进内存
        OutputStream os = response.getOutputStream();   //创建字节输出流
        int len = 0;
        byte[] buffer = new byte[1024];
        while((len = is.read(buffer))!=-1) {
            os.write(buffer,0,len);     //向浏览器写入
        }
        os.close();
        is.close();
    }

    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){

        try {
            List<Map> list =  orderSettingService.findOrderSetting(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("editNumberByDate")
    public Result editNumber(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

}
